<?php
defined('IN_TS') or die('Access Denied.');

$page = isset($_GET['page']) ? intval($_GET['page']) : 1;


$duty = intval($_GET['duty']);
$groupid = intval($_GET['groupid']);
$orgid = intval($_GET['orgid']);

if($groupid){
    $group = aac('group')->getOneGroup($groupid);
    $arrData = 'groupid='.$groupid;
}

if($orgid){
    if($groupid){
        $arrUserid = aac('user')->findAll('user_info',array('orgid'=>$orgid));
        foreach($arrUserid as $item){
            $userids[]=$item['userid'];
        }
        $userids = '('.implode(',',$userids).')';
        $arrData =$arrData." and userid in ".$userids;
    }else{
        $arrData = 'orgid='.$orgid;
    }

    $org = aac('user')->find('user_org',array('orgid'=>$orgid));
}else{
    if(!$groupid){
        $arrData = 'groupids<>""';
    }
}

if($duty == 1){
    $arrData = $arrData." and isvo=1 and count_duty > 0";
    $title = '上岗';
}
if($duty == 2){
    $arrData = $arrData." and isvo=1 and count_duty > 0 and lasttime>=".strtotime('-4 week');
    $title = '活跃';
}
if($duty == 3){
    $arrData = $arrData." and isvo=1 and count_duty > 0 and lasttime<".strtotime('-4 week');
    $title = '潜水';
}

if($duty == 4){
    if($groupid){
        $arrUserid = aac('user')->findAll('user_info',array('isrenzheng'=>1));
        foreach($arrUserid as $item){
            $userids[]=$item['userid'];
        }
        $userids = '('.implode(',',$userids).')';
        $arrData =$arrData." and isvo = 1 and count_duty = 0  and userid in ".$userids;
    }else{
        $arrData = $arrData." and isvo=1 and count_duty = 0 and isrenzheng=1";
    }

    $title = '候任';
}
if($duty == 5){
    if($groupid){
        $arrUserid = aac('user')->findAll('user_info',array('isrenzheng'=>0));
        foreach($arrUserid as $item){
            $groupids = explode(',',$item['groupids']);
            if(in_array($groupid,$groupids)){
                $userids[]=$item['userid'];
            }
        }
        $userids = '('.implode(',',$userids).')';
        $arrData =$arrData." and isvo = 1 and count_duty = 0 and userid in ".$userids;
    }else {
        $arrData = $arrData . " and isvo=1 and count_duty = 0 and isrenzheng=0";
    }
    $title = '候选';
}
if($duty == 6){
    $arrData = $arrData." and (isstock=1 or isstock=2)";
    $title = '馆东';
}
if($duty == 7){
    if($groupid){
        $arrUserid = aac('user')->findAll('user_info',array('iscoach'=>1));
        foreach($arrUserid as $item){
            $userid[]=$item['userid'];
        }
        $userids = '('.implode(',',$userid).')';
        $arrData =$arrData." and isvo=1 and userid in ".$userids;
    }else {
        $arrData = $arrData . "  and iscoach=1";
    }
    $title = '教练';
}
if($duty == 8){
    $arrData = $arrData."   and leadership>0";
    $title = '理事、馆长储备';
}


if($title == ''){
    $title = '全体';
}
$title = $group['groupname'].$org['orgname'].$title;

$strGroup = aac('group')->findAll('group','isplace>0','isplace desc,province asc,addtime asc');

$strOrg = aac('user')->findAll('user_org');

$lstart = $page*30-30;

$url = SITE_URL.'index.php?app=user&ac=list&groupid='.$groupid.'&orgid='.$orgid.'&duty='.$duty.'&page=';

if($groupid){
    $arrUser = aac('group')->findAll('group_user',$arrData,'lasttime desc, uptime desc','',$lstart.',30');
    $userNum = aac('group')->findCount('group_user',$arrData);
}else{
    $arrUser	= $new['user']->findAll('user_info',$arrData,'lasttime desc, uptime desc','',$lstart.',30');
    $userNum = $new['user']->findCount('user_info',$arrData);
}

foreach($arrUser as $k=>$item){
    $arrAllUser[$k] = $new['user']->getOneUser($item['userid']);
    if($groupid){
        $user = aac('group')->find('group_user',array('groupid'=>$groupid,'userid'=>$item['userid']));
        $arrAllUser[$k]['count_duty']=$user['count_duty'];
        $arrAllUser[$k]['lasttime']=$user['lasttime'];
        $arrAllUser[$k]['leadership']=$user['leadership'];
    }
}



$pageUrl = pagination($userNum, 30, $page, $url);

$title = $title.'成员';

include template("list");
